#!/usr/bin/env python

import gtk, gettext, gobject
from blend import constants
import constants as iconstants
from signals import traySignals
from signals import commonSignals
from mxit import handles

gettext.install('blendIT','locale');

class tray(gtk.StatusIcon):

   def __init__(self,blend):
      gtk.StatusIcon.__init__(self);

      self.blend = blend;
      self.sigs = traySignals.signals(blend);
      self.csigs = commonSignals.signals(blend);
      
      self.set_from_file('images/blendit.png');
      ttp = "BlendIT - %s";
      ttp = ttp % constants.C.presences[handles.Presence.OFFLINE].text;
      self.set_tooltip(ttp);

      self.menu = self.drawMenu();
      self.connect('popup-menu',self.displayMenu);
      self.connect('activate',self.sigs.on_click);     

   def set_tooltip(self,text):
      self.tooltipText = text;
      gtk.StatusIcon.set_tooltip(self,text);

   def get_tooltip(self):
      return self.tooltipText;

   def changeTooltip(self,presence=None,mood=None):
      tooltip = "BlendIT - ";
      tip = self.get_tooltip()[10:];
      
      seps = tip.split(", ");
      if len(seps) == 1:
         if presence != None:
            tooltip += presence.text;
            
         if mood != None:
            tooltip += seps[0] + ", " + mood.text; 
      else:
         # Both presence and mood
         pres = "";
         mod = "";
         if presence == None:
            pres = seps[0];
         else:
            pres = presence.text;

         if mood == None:
            mod = seps[1];
         else:
            mod = mood.text;

         tooltip += pres + ", " + mod;

         try:
            if mood.const is handles.Mood.NONE:
               tooltip = tooltip.split(", ")[0];
         except AttributeError:
            #tooltip = tooltip.split(", ")[0];
            pass;
      
      self.set_tooltip(tooltip);          

   def changePresence(self,presence):
      self.changeTooltip(presence=presence);
      img = gtk.Image();
      pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(presence.image,16,16);
      img.set_from_pixbuf(pixbuf);
      self.currentPresenceMenuItem.set_image(img);
      self.set_from_file(presence.timage);

   def changeMood(self,mood):
      self.changeTooltip(mood=mood);
      img = gtk.Image();
      pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(mood.image,22,22);
      img.set_from_pixbuf(pixbuf);
      self.currentMoodMenuItem.set_image(img);

   def displayMenu(self,statusicon,button,time):
      self.menu.popup(None,None,gtk.status_icon_position_menu,button,time,statusicon);

   def createMoodSubmenu(self):
      moodSubMenu = gtk.Menu();
      moods = constants.C.moods.values();
      moods.sort(key=self._alphabetise);
      
      for i in range(0,len(moods)):        
         menuItem = gtk.ImageMenuItem(moods[i].text);
         menuImage = gtk.Image();
         pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(moods[i].image,22,22);
         menuImage.set_from_pixbuf(pixbuf);
         menuItem.set_image(menuImage);
         menuItem.connect("activate",self.sigs.on_select_mood,moods[i]);
         menuItem.show();
         moodSubMenu.append(menuItem);
         
      moodSubMenu.show();
      return moodSubMenu;

   def _alphabetise(self,mood):
      return mood.sort.lower();

   def createPresenceSubmenu(self):
      moodSubMenu = gtk.Menu();
      presences = constants.C.orderedPresences;
      for i in range(0,len(presences)):
         menuItem = gtk.ImageMenuItem(presences[i].text);
         menuImage = gtk.Image();
         pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(presences[i].image,
                                                       16,16);
         menuImage.set_from_pixbuf(pixbuf);
         menuItem.set_image(menuImage);
         menuItem.connect("activate",self.sigs.on_select_presence,
                          presences[i]);
         menuItem.show();
         moodSubMenu.append(menuItem);
         
      moodSubMenu.show();
      return moodSubMenu;

   def refreshIMSubmenu(self):
      self.imMenuItem.set_submenu(self.createIMSubmenu());

   def createIMSubmenu(self):
      submenu = gtk.Menu();
      buddies = self.blend.conn.roster.all_online_buddies();
      for i in range(0,len(buddies)):
         presence = constants.C.presences[buddies[i].presence];
         menuItem = gtk.ImageMenuItem(buddies[i].name);
         menuImage = gtk.Image();
         pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(presence.image,16,16);
         menuImage.set_from_pixbuf(pixbuf);
         menuItem.set_image(menuImage);
         menuItem.connect("activate",self.sigs.on_new_im,buddies[i]);
         menuItem.show();
         submenu.append(menuItem);
      if len(buddies) == 0:
         menuItem = gtk.MenuItem(_("No online buddies"));
         menuItem.set_sensitive(False);
         menuItem.show();
         submenu.append(menuItem);
      return submenu;

   # Draw the context menu for the tray icon
   def drawMenu(self):
      menu = gtk.Menu();

      self.displayBuddyListMenuItem = gtk.CheckMenuItem(_("_Show Buddy List"));
      self.displayBuddyListMenuItem.connect('toggled',self.sigs.on_show_buddy_list);
      self.displayBuddyListMenuItem.set_active(True);
      self.displayBuddyListMenuItem.show();
      menu.append(self.displayBuddyListMenuItem);
      

      # Separator
      separator1 = gtk.SeparatorMenuItem();
      separator1.show();
      menu.append(separator1);

      # Change Mood Menu Item
      self.currentMoodMenuItem = gtk.ImageMenuItem(_("Change _Mood"));
      currentMoodImage = gtk.Image();
      pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(iconstants.IMG_None,18,18);
      currentMoodImage.set_from_pixbuf(pixbuf);
      self.currentMoodMenuItem.set_image(currentMoodImage);
      self.currentMoodMenuItem.set_submenu(self.createMoodSubmenu());
      self.currentMoodMenuItem.show();
      menu.append(self.currentMoodMenuItem);
      
      # Change Presence Menu Item
      self.currentPresenceMenuItem = gtk.ImageMenuItem(_("Change _Presence"));
      currentPresenceImage = gtk.Image();
      pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(iconstants.IMG_Offline,16,16);
      currentPresenceImage.set_from_pixbuf(pixbuf);
      self.currentPresenceMenuItem.set_image(currentPresenceImage);
      self.currentPresenceMenuItem.set_submenu(self.createPresenceSubmenu());
      self.currentPresenceMenuItem.show();
      menu.append(self.currentPresenceMenuItem);
      
      # Separator
      separator2 = gtk.SeparatorMenuItem();
      separator2.show();
      menu.append(separator2);

      # New IM (This has a list of online, away or busy buddies)
      self.imMenuItem = gtk.ImageMenuItem(_("Instant _Messenge"));
      imMenuImage = gtk.Image();
      imMenuImage.set_from_stock(gtk.STOCK_NEW,gtk.ICON_SIZE_MENU);
      self.imMenuItem.set_image(imMenuImage);
      self.imMenuItem.set_submenu(self.createIMSubmenu());
      self.imMenuItem.show();
      menu.append(self.imMenuItem);

      # New MultiMi
      mmMenuItem = gtk.ImageMenuItem(_("_New MultiMx"));
      mmMenuImage = gtk.Image();
      pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(iconstants.IMG_MultiMx,16,16);
      mmMenuImage.set_from_pixbuf(pixbuf);
      mmMenuItem.set_image(mmMenuImage);
      mmMenuItem.connect('activate',self.csigs.on_new_multimx);
      mmMenuItem.show();
      menu.append(mmMenuItem);

      # Separator
      separator3 = gtk.SeparatorMenuItem();
      separator3.show();
      menu.append(separator3);
      
      # Quit Menu Item
      quitMenuItem = gtk.ImageMenuItem(_("_Quit"));
      quitImage = gtk.Image();
      quitImage.set_from_stock(gtk.STOCK_QUIT,gtk.ICON_SIZE_MENU);
      quitMenuItem.set_image(quitImage);
      quitMenuItem.connect('activate',self.csigs.on_quit);
      quitMenuItem.show();
      menu.append(quitMenuItem);
      
      return menu;
      